//
//  ViewController4.m
//  nmrsc
//
//  Created by xumckay on 16/1/9.
//  Copyright © 2016年 bric. All rights reserved.
//

#import "MyPersonCenterViewController.h"
#import "personHearView.h"
#import "MyResource.h"
#import "myOrderCell.h"
#import "moneyAndSeverCell.h"
#import "otherCell.h"
#import "PersonalDetailviewController.h"
#import "FeedbackViewController.h"
#import "SettingViewController.h"
#import "LoginViewController.h"
@interface MyPersonCenterViewController ()

@property (strong,nonatomic)UITableView *tableView;
@property (strong,nonatomic)NSArray *dataArr;
@property (strong,nonatomic)LoginViewController *loginVC;

@end

@implementation MyPersonCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=COLOR(245, 245, 245, 1);
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,kDeviceWidth,kDeviceHeight)];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor=COLOR(245, 245, 245, 1);
    [self.view addSubview:_tableView];
    MyResource *r1=[[MyResource alloc]initWithHeaderName:@"我的订单" withHeaderImage:@"myorder" withButtonArray:nil];
    MyResource *r2=[[MyResource alloc]initWithHeaderName:@"我的资金" withHeaderImage:@"mymoney" withButtonArray:nil];
    MyResource *r3=[[MyResource alloc]initWithHeaderName:@"服务中心" withHeaderImage:@"serverCenter" withButtonArray:nil];
    MyResource *r4=[[MyResource alloc]initWithHeaderName:@"设置管理" withHeaderImage:@"setting" withButtonArray:nil];
    _dataArr=@[r1,r2,r3,r4];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getSelfInfo) name:kGetSelfInfo object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginOutBtnClicked) name:@"loginOutBtnClicked" object:nil];
    
}

-(void)addtableHeader
{
    personHearView *headerView=[[personHearView alloc]initWithFrame:CGRectMake(0,0,kDeviceWidth,111)];
    [headerView.managerAddressBtn addTarget:self action:@selector(managerAddressBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(IntoPersonalDetailView)];
    [headerView addGestureRecognizer:tap];
    _tableView.tableHeaderView=headerView;
}

//登陆成功后刷新数据
-(void)getSelfInfo
{
    [self addtableHeader];
}


#pragma mark    --TableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier1=@"myOrder";
    static NSString *identifier2=@"moneyANDsever";
    static NSString *identifier3=@"other";
    MyResource *resource=[_dataArr objectAtIndex:indexPath.row];

    if (indexPath.row==0) {
        myOrderCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell) {
            cell=[[myOrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell.titleBtn setTitle:resource.aName forState:UIControlStateNormal];
        [cell.titleBtn setImage:[UIImage imageNamed:resource.aImage] forState:UIControlStateNormal];
        
        [cell.waitForPayBtn addTarget:self action:@selector(waitForPayBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [cell.waitForGetBtn addTarget:self action:@selector(waitForGetBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [cell.waitForTalkBtn addTarget:self action:@selector(waitForTalkBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [cell.waitForByeByeBtn addTarget:self action:@selector(waitForByeByeBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [cell.showAllOrderListBtn addTarget:self action:@selector(showAllOrderListBtnClicked) forControlEvents:UIControlEventTouchUpInside];
         return cell;
        
    }else
        if(indexPath.row==1||indexPath.row==2)
    {
        moneyAndSeverCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier2];
        if (!cell) {
            cell=[[moneyAndSeverCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier2];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell.titleBtn setTitle:resource.aName forState:UIControlStateNormal];
        [cell.titleBtn setImage:[UIImage imageNamed:resource.aImage] forState:UIControlStateNormal];
        if (indexPath.row==1) {
            [cell.btn1 setTitle:@"红包" forState:UIControlStateNormal];
            [cell.btn2 setTitle:@"优惠券" forState:UIControlStateNormal];
            [cell.btn3 setTitle:@"交易记录" forState:UIControlStateNormal];
            cell.btn1.tag=101;
            cell.btn2.tag=102;
            cell.btn3.tag=103;

        }else
        {
            [cell.btn1 setTitle:@"售后服务" forState:UIControlStateNormal];
            [cell.btn2 setTitle:@"意见反馈" forState:UIControlStateNormal];
            [cell.btn3 setTitle:@"常见问题" forState:UIControlStateNormal];
            cell.btn1.tag=104;
            cell.btn2.tag=105;
            cell.btn3.tag=106;
        }
        [cell.btn1 addTarget:self action:@selector(firstBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn2 addTarget:self action:@selector(secondBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn3 addTarget:self action:@selector(thirdBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
        return cell;
    }
    else if (indexPath.row==3)
    {
        otherCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier3];
        if (!cell) {
            cell=[[otherCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier3];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell.titleBtn setTitle:resource.aName forState:UIControlStateNormal];
        [cell.titleBtn setImage:[UIImage imageNamed:resource.aImage] forState:UIControlStateNormal];
        [cell.titleBtn addTarget:self action:@selector(showSettingView) forControlEvents:UIControlEventTouchUpInside];
        [cell.telePhone addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 111;
    
}

-(void)IntoPersonalDetailView
{
    NSLog(@"进入个人资料界面");
    PersonalDetailviewController *personVC=[[PersonalDetailviewController alloc]init];
    [self.navigationController pushViewController:personVC animated:YES];
}

//管理收货地址
-(void)managerAddressBtnClicked
{
    NSLog(@"0");
}

//查看全部订单
-(void)showAllOrderListBtnClicked
{
    NSLog(@"查看全部订单");
}

//待付款
-(void)waitForPayBtnClicked
{
    
    NSLog(@"1");
}
//待收货
-(void)waitForGetBtnClicked
{
    NSLog(@"2");

    
}
//待评价
-(void)waitForTalkBtnClicked
{
    NSLog(@"3");

}
//退货
-(void)waitForByeByeBtnClicked
{
    NSLog(@"4");

}
//红包或售后服务
-(void)firstBtnClicked:(UIButton *)sender
{
    if (sender.tag==101) {                  //红包
        NSLog(@"5");

    }
    else if (sender.tag==104)               //售后服务
    {
        NSLog(@"8");

    }
    
}
//优惠券或意见反馈
-(void)secondBtnClicked:(UIButton *)sender
{
    if (sender.tag==102) {                 //优惠券
        NSLog(@"6");

    }
    else if (sender.tag==105)               //意见反馈
    {
        NSLog(@"9");
        FeedbackViewController *feedback=[[FeedbackViewController alloc]init];
        [self.navigationController pushViewController:feedback animated:YES];
    }
    
}
//交易记录或常见问题
-(void)thirdBtnClicked:(UIButton *)sender
{
    if (sender.tag==103) {                 //交易记录
        NSLog(@"7");

    }
    else if (sender.tag==106)               //常见问题
    {
        NSLog(@"10");

    }
}

//打电话
-(void)callPhone
{
    NSLog(@"11");
    UIWebView*callWebview =[[UIWebView alloc] init];
    NSURL *telURL =[NSURL URLWithString:@"tel://4001698801"];// 貌似tel:// 或者 tel: 都行
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    //记得添加到view上
    [self.view addSubview:callWebview];

}

//点击设置管理界面
-(void)showSettingView
{
    NSLog(@"点击设置管理界面");
    SettingViewController *settingVC=[[SettingViewController alloc]init];
    [self.navigationController pushViewController:settingVC animated:YES];
}

//退登以后把登陆界面放上去
-(void)loginOutBtnClicked
{
    if (!_loginVC) {
        _loginVC=[[LoginViewController alloc]init];
        _loginVC.view.frame=CGRectMake(0,0,kDeviceWidth,kDeviceHeight);
    }
    [self.view addSubview:_loginVC.view];
    [self addChildViewController:_loginVC];
}


@end
